package com.x.provider.oss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationResponse;
import com.x.core.exception.ApiException;
import com.x.core.utils.ApiAssetUtil;
import com.x.provider.api.oss.enums.CloudErrorCode;
import com.x.provider.oss.configure.QqConfig;
import com.x.provider.oss.mapper.IdCardMapper;
import com.x.provider.oss.model.domain.IdCard;
import com.x.provider.oss.model.query.IdCardQuery;
import com.x.provider.oss.service.FaceIdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author: liushenyi
 * @date: 2023/02/18/14:41
 */
@Slf4j
@Service
public class FaceIdServiceImpl implements FaceIdService {

    private final IdCardMapper idCardMapper;
    private final QqConfig tencentOssConfig;

    private boolean supportFaceId = false;

    public FaceIdServiceImpl(IdCardMapper idCardMapper,
                             QqConfig tencentOssConfig){
        this.idCardMapper = idCardMapper;
        this.tencentOssConfig = tencentOssConfig;
    }

    @Override
    public void idCardVerification(String idCard, String name) {
        final IdCard idCardEntity = get(IdCardQuery.builder().idCard(idCard).name(name).build());
        if (idCardEntity != null){
            return;
        }
        Boolean result = idCardVerificationRemote(idCard, name);
        if (result && supportFaceId){
            idCardMapper.insert(IdCard.builder().idCard(idCard).name(name).build());
        }
        ApiAssetUtil.isTrue(result, CloudErrorCode.ID_CARD_VERIFICATION_NOT_MATCH);
    }

    private IdCard get(IdCardQuery idCardQuery){
        return idCardMapper.selectOne(build(idCardQuery));
    }

    private LambdaQueryWrapper<IdCard> build(IdCardQuery query){
        LambdaQueryWrapper<IdCard> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(query.getIdCard())){
            queryWrapper = queryWrapper.eq(IdCard::getIdCard, query.getIdCard());
        }
        if (!StringUtils.isEmpty(query.getName())){
            queryWrapper = queryWrapper.eq(IdCard::getName, query.getName());
        }
        return queryWrapper;
    }

    public Boolean idCardVerificationRemote(String idCard, String name) {
        try {
            if (!supportFaceId){
                return true;
            }
            FaceidClient client = new FaceidClient(new Credential(tencentOssConfig.getAppSecretId(), tencentOssConfig.getAppSecretKey()), QqConfig.AP_CHENGDU);
            IdCardVerificationRequest request = new IdCardVerificationRequest();
            request.setIdCard(idCard);
            request.setName(name);
            IdCardVerificationResponse response = client.IdCardVerification(request);
            return response.getResult().equals("0") ? true : false;
        }
        catch (TencentCloudSDKException e){
            log.error(e.getMessage(), e);
            throw new ApiException(e.getMessage());
        }
    }

}
